package com.shihb.chapter06customercrm.view

import com.shihb.chapter06customercrm.bean.Customer

import scala.io.StdIn
import com.shihb.chapter06customercrm.service._

import scala.collection.mutable.ListBuffer

/**
 * Description:
 *
 * @author shihb
 * @date 2020/10/2 16:08
 */
class CustomerView {
  //定义一个循环变量,控制是否退出
  var loop: Boolean = true
  // 获取用户输入的值
  var key: Char = _
  val service: CustomerService = new CustomerService

  //定义一个key
  def mainMenu: Unit = {
    do {
      println("------客户信息管理系统--------")
      println("------1.添加客户")
      println("------2.修改客户")
      println("------3.删除客户")
      println("------4.客户列表")
      println("------5.退    出")
      println("请选择(1-5)")
      key = StdIn.readChar()
      key match {
        case '1' => add
        case '2' => println("修改客户")
        case '3' => delete
        case '4' => list
        case '5' => loop = false
        case _ =>
      }
    } while (loop)

  }

  def list: Unit = {
    println("-------------------用户列表-----------------------")
    println("编号  性名  性别  年龄    电话           邮箱")
    for (o <- this.service.list) {
      println(o)
    }
    println("-------------------------------------------------")
  }

  def add: Unit = {
    println("-------------------添加用户-----------------------")
    println("姓名:")
    val name: String = StdIn.readLine()
    println("性别:")
    val gender: Char = StdIn.readChar()
    println("年龄:")
    val age: Short = StdIn.readShort()
    println("电话:")
    val tel: String = StdIn.readLine()
    println("邮箱:")
    val email: String = StdIn.readLine()

    val bool: Boolean = service.add(new Customer(name, gender, age, tel, email))
    println(s"-----------------添加" + {
      if (bool) "成功" else "失败"
    } + "-----------")
  }

  def delete: Unit = {
    println("-------------------删除用户-----------------------")
    println("请输入要删除的用户id(-1退出)：")
    val id: Int = StdIn.readInt()
    if (id == -1) {
      println("-------------------------------------------------")
      return
    }
    println("是否确认删除(Y/N)：")
    val choice: Char = StdIn.readChar().toLower
    var bool: Boolean = false
    if (choice == 'y') {
      bool = service.delete(id)
    }
    println(s"-----------------删除" + {
      if (bool) "成功" else "失败"
    } + "-----------")
  }

}
